package bb.lanxing.network;

import org.litepal.LitePal;

import bb.lanxing.model.database.BiciCookie;
import bb.lanxing.util.Log;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

public class CookieJarImpl implements CookieJar {
    @Override
    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
        if (!httpUrl.toString().contains(getDomain()) || list == null) {
            return;
        }
        for (Cookie cookie : list) {
            if (!cookie.name().isEmpty() && !cookie.value().isEmpty()) {
                BiciCookie byName = BiciCookie.getByName(cookie.name());
                if (byName == null) {
                    byName = new BiciCookie();
                }
                byName.setName(cookie.name());
                byName.setValue(cookie.value());
                byName.save();
            }
        }
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
        List<BiciCookie> listAll;
        ArrayList arrayList = new ArrayList();
//        if (httpUrl != null) {
//            String httpUrl2 = httpUrl.toString();
//            Log.e("domain*****", getDomain());
//            if (httpUrl2.contains(getDomain()) && (listAll = BiciCookie.listAll(BiciCookie.class)) != null) {
//                for (BiciCookie biciCookie : listAll) {
//                    arrayList.add(new Cookie.Builder().name(biciCookie.getName()).value(biciCookie.getValue()).domain(httpUrl.host()).build());
//                }
//            }
//        }
        return arrayList;
    }

    protected String getDomain() {
        return MyHttpClient.DOMAIN_BASE_PURE;
    }

    public static void clear() {
        LitePal.deleteAll(BiciCookie.class);
//        BiciCookie.deleteAll(BiciCookie.class);
    }


}
